﻿using ErrorOr;
using Gym.Application.Common.Interfaces;
using Gym.Domain.SessionAggregate;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gym.Application.Participants.Queries.ListParticipantSessions
{
    public class ListParticipantSessionsQueryHandler : IRequestHandler<ListParticipantSessionsQuery, ErrorOr<List<Session>>>
    {
        private readonly ISessionsRepository _sessionsRepository;
        private readonly IParticipantsRepository _participantsRepository;

        public ListParticipantSessionsQueryHandler(ISessionsRepository sessionsRepository, IParticipantsRepository participantsRepository)
        {
            _sessionsRepository = sessionsRepository;
            _participantsRepository = participantsRepository;
        }
        public async Task<ErrorOr<List<Session>>> Handle(ListParticipantSessionsQuery request, CancellationToken cancellationToken)
        {
            var participant = await _participantsRepository.GetByIdAsync(request.ParticipantId);
            if (participant is null)
            {
                return Error.NotFound(description: "没有找到该用户信息");
            }

            return await _sessionsRepository.ListByIdsAsync(participant.SessionIds, request.StartDateTime, request.EndDateTime);
        }
    }
}
